package com.david.videoapp.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.app.PictureInPictureParams
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import androidx.media3.common.MediaItem
import androidx.media3.exoplayer.DefaultRenderersFactory
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
import androidx.media3.ui.PlayerView
import com.alibaba.fastjson.JSON
import com.david.core.utils.PreferenceUtils
import com.david.videoapp.exoplayer.BlackListMediaCodecSelector

class SystemVideoActivity:Activity() {
//    lateinit var title:TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val intent = intent
        val action = intent.action
        if (Intent.ACTION_VIEW == action) {
            val uri = intent.data

            val localView = getVideoView()
            localView.layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)
            setContentView(localView)
//            exoPlayer!!.setMediaItem(MediaItem.Builder().setUri("http://39.135.135.37:80/wh7f454c46tw2588615213_-1035451685/ottrrs.hl.chinamobile.com/PLTV/88888888/224/3221225743/index.m3u8?icpid=88888888&RTS=1583531260&from=1&hms_devid=683").build())
            exoPlayer!!.setMediaItem(MediaItem.Builder().setUri(uri).build())

            exoPlayer!!.playWhenReady = true
            exoPlayer!!.prepare()
        }
//        title = TextView(this)
//        title.layoutParams = ViewGroup.LayoutParams(150, 150)
//        title.setOnClickListener(object :View.OnClickListener{
//            override fun onClick(v: View?) {
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                        var builder = PictureInPictureParams.Builder()
////                        builder.setSourceRectHint()
//                        enterPictureInPictureMode(PictureInPictureParams.Builder().build())
//                    } else {
//                        enterPictureInPictureMode();
//                    }
//                }
//            }
//
//        })
//        title.text = "画中画模式"
//        findViewById<ViewGroup>(android.R.id.content).addView(title)
    }

    private lateinit var localPlayerView: PlayerView
    private var exoPlayer: ExoPlayer? = null

    @SuppressLint("UnsafeOptInUsageError")
    private fun getVideoView(): View {
        localPlayerView = PlayerView(this)
        localPlayerView.requestFocus()

        exoPlayer = ExoPlayer.Builder(
            this,
            DefaultRenderersFactory(this).setMediaCodecSelector(BlackListMediaCodecSelector()),
            DefaultMediaSourceFactory(this)
        ).build()
        localPlayerView.player = exoPlayer

        localPlayerView.hideController()
        return localPlayerView
    }

    override fun onResume() {
        super.onResume()
        exoPlayer?.play()
    }

    override fun onStop() {
        super.onStop()
        exoPlayer?.pause()
    }

    /**用户按下home键即将回到桌面时回调，返回时不回调*/
    override fun onUserLeaveHint() {
        super.onUserLeaveHint()
        Log.i("video", "onUserLeaveHint...")
        enterPictureInPictureMode(PictureInPictureParams.Builder().build())
    }

    override fun onPictureInPictureModeChanged(
        isInPictureInPictureMode: Boolean,
        newConfig: Configuration?
    ) {
        super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig)
        Log.i("video", "onPictureInPictureModeChanged..." + isInPictureInPictureMode +" " + JSON.toJSONString(newConfig))

//        if (isInPictureInPictureMode) {//小窗模式
//            title.visibility = View.GONE
//        } else {//全屏模式
//            title.visibility = View.VISIBLE
//        }
    }

    override fun onDestroy() {
        super.onDestroy()
        PreferenceUtils.getInstance().setLong("", exoPlayer!!.currentPosition)
        exoPlayer?.release()
    }
}